/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.dataacesslayer;

import java.sql.*;
import java.util.logging.*;

/**
 *
 * @author lee
 */
public class DataExecute extends DataConnection{
    
    private Statement m_statement = null; 
    private PreparedStatement m_preStatement = null;
    private String    m_StrCmd;
    //Use with StoreProcedure
    private CallableStatement m_callStatement;
    
    public DataExecute() throws SQLException
    {
        super();
    }
    
    private boolean createStatement() // Use if the query contains no parameters
    {
        try
        {
            this.m_statement = this.m_connect.createStatement();
            return true;
        } 
        catch (SQLException ex) {
            Logger.getLogger(DataExecute.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    
    private boolean createPreparedStatement(String _strCmd) // Use if the query contains parameters
    {
        try
        {
            this.m_preStatement = this.m_connect.prepareStatement(_strCmd);
            return true;
        }
        catch(SQLException ex)
        {
            return false;
        }
    }
    
    public ResultSet getDataFromDatabaseNoParameters(String _strCmd) // Method will return ResultSet --- the same Datatable in c#
    {
        if(createStatement())
        {
            try {
                this.m_StrCmd = _strCmd;
                return this.m_statement.executeQuery(_strCmd);
                //You can use code:
                //PreparedStatement statement = this.m_connect.prepareStatement(_strCmd);
                //ResultSet result = this.m_statement.executeQuery();
                //Use DatabaseMetaData.supportsResultSetType(int type)check your database and JDBC driver ResultType
            } catch (SQLException ex) {
                return null;
            }
        }
        else
        {
            return null;
        }

    }
    
    public ResultSet getDataFromDatabase(String _strCmd) 
    {
        if(createPreparedStatement(_strCmd))
        {
            try {
                this.m_StrCmd = _strCmd;
                return this.m_preStatement.executeQuery();
                //You can use code:
                //PreparedStatement statement = this.m_connect.prepareStatement(_strCmd);
                //ResultSet result = this.m_statement.executeQuery();
                //Use DatabaseMetaData.supportsResultSetType(int type)check your database and JDBC driver ResultType
            } catch (SQLException ex) {
                return null;
            }
        }
        else
        {
            return null;
        }

    }
    
    public int updateDataToDatabase(String _strCmd)
    {
        if(createPreparedStatement(_strCmd))
        {
            try
            {
               this.m_StrCmd = _strCmd;
               return this.m_statement.executeUpdate(_strCmd);
            }
            catch(SQLException ex)
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }
    

}
